/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.swirrel.installer;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ContainerListener;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
import org.swirrel.annotation.ComponentAdded;
import org.swirrel.annotation.ComponentRemoved;
import org.swirrel.listener.SwirrelContainerListener;
import org.swirrel.util.MethodCall;

/**
 *
 * @author Gronau
 */
public class ContainerListenerInstaller extends AbstractListenerInstaller implements SwirrelListenerInstaller {

    public ContainerListenerInstaller() {
        super(Arrays.<Class<? extends Annotation>>asList(ComponentAdded.class, ComponentRemoved.class),
                Arrays.<Class<?>>asList(Container.class));
    }

    public boolean isListenerAlreadySet(Object obj) {
       ContainerListener[] containerListeners = ((Container) obj).getContainerListeners();
        for (ContainerListener cl : containerListeners) {
            if (cl instanceof SwirrelContainerListener) {
                return true;
            }
        }
        return false;
    }

    public void registerListener(Field field, Component comp, java.util.List<MethodCall> methodCalls) throws IllegalAccessException {
        Object obj = field.get(comp);
        ContainerListener cl = new SwirrelContainerListener(methodCalls.get(0), methodCalls.get(1));
         ((Container) obj).addContainerListener(cl);
    }
}
